const express = require('express');
const mysql = require('mysql');
const multer = require('multer');
const path = require('path');
const router = express.Router();
const address = require('../MySql/index');
const sql = mysql.createConnection(address.mysql);

// 配置 multer 存储选项
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/'); // 设置文件存储路径
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + path.extname(file.originalname)); // 设置文件名
    }
});

const upload = multer({ storage: storage });

sql.connect(function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('链接成功');
    }
});

// 获取机构列表
router.get('/getList', (req, res) => {
    sql.query('SELECT * FROM institution', (err, result) => { // 修正了表名拼写错误
        if (err) {
            console.log(err);
        } else {
            res.send({
                code: 200,
                data: result,
                success: true
            });
        }
    });
});

// 上传文件路由
// router.post('/uploadFiles', upload.fields([{ name: 'templateFile', maxCount: 1 }, { name: 'paramFile', maxCount: 1 }]), (req, res) => {
//     if (!req.files || Object.keys(req.files).length === 0) {
//         return res.status(400).send('No files were uploaded.');
//     }

//     const templateFile = req.files['templateFile'][0];
//     const paramFile = req.files['paramFile'][0];

//     // 读取文件内容
//     const templateFileContent = fs.readFileSync(templateFile.path);
//     const paramFileContent = fs.readFileSync(paramFile.path);

//     // 将文件内容存储到数据库
//     const query = 'INSERT INTO  (template_file, param_file) VALUES (?, ?)';
//     sql.query(query, [templateFileContent, paramFileContent], (err, result) => {
//         if (err) {
//             console.log(err);
//             res.status(500).send('Error uploading files.');
//         } else {
//             res.send({
//                 code: 200,
//                 message: 'Files uploaded successfully.',
//                 success: true
//             });
//         }
//     });
// });

module.exports = router;